package com.gyc.simple.userservices.controller;


import com.gyc.simple.common.util.DateUtils;
import com.gyc.simple.common.util.MD5Utils;
import com.gyc.simple.common.util.UUIDUtils;
import com.gyc.simple.userservices.entity.base.IUser;
import com.gyc.simple.userservices.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 
 */
@RequestMapping("/user")
@RestController
public class UserController {

	@Autowired
	private IUserService userService;

    /**
     * 新增用户
     */
    @PostMapping("/addUser")
    public String addUser(IUser iuser){
        try {
            if(iuser!=null){
                iuser.setIuId(UUIDUtils.getUUID32());
                Date date = new Date();
                iuser.setIuRegistertime(DateUtils.getTimestamp(date));
                String info = iuser.getIuPassword()+ DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss");
                iuser.setIuPassword(MD5Utils.getMD5String(info));
                userService.addUser(iuser);
            }
        }catch (Exception e){
            e.printStackTrace();
            return "false";
        }
        return "ok";
    }
	
	/**
	 * 获取配置文件中系统默认用户
	 * @return
	 */
    @GetMapping("/getDefaultUser")
    public String getDefaultUser(){
        return userService.getDefaultUser();
    }
    
    /**
     * 获取上下文用户
     * @return
     */
    @GetMapping("/getContextUserId")
    public String getContextUserId(){
        return userService.getContextUserId();
    }
    
    /**
     * 获取供应商数据
     * @return
     */
    @GetMapping("/getProviderData")
    public List<String> getProviderData(){
        return userService.getProviderData();
    }
}
